package com.rd.student.dashuju.sunwenzhuo.Test2;
import java.util.concurrent.locks.ReentrantLock;

public class TestBank3 implements Runnable {
    static Account3 account3;
    static double money;

    static ReentrantLock lock = new ReentrantLock();
    public TestBank3(Account3 account3, double money) {
        this.account3 = account3;
        this.money = money;
    }

    @Override
    public  void run() {
        lock.lock();
        if (account3.getBalance() < money) {
        System.out.println("余额不足");
        return;
    }
        System.out.println(Thread.currentThread().getName() + "可以取钱");
        account3.setBalance(account3.getBalance() - money);
        System.out.println("取钱后的余额为：" + account3.getBalance());
        lock.unlock();
    }


    public static void main(String[] args) {
        Account3 account3 = new Account3("123456", 888);
        Runnable runnable1 = new TestBank3(account3, 666);
        Runnable runnable2 = new TestBank3(account3, 888);
        Thread thread1 = new Thread(runnable1,"张三");
        Thread thread2 = new Thread(runnable2,"李四");
        thread1.start();
        thread2.start();

    }
}

class Account3 {
    private String accountId;

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    private double balance;

    // 创建构造器

    public Account3(String accountId, double balance) {
        this.accountId = accountId;
        this.balance = balance;
    }
}
